//
//  MXEquipmentNoticeController.m
//  HNBaseProject
//
//  Created by xujie on 2024/6/4.
//

#import "MXEquipmentNoticeController.h"
#import "MXMessageListCell.h"
#import "MXHomeInviteController.h"
#import "DHNoDataView.h"

@interface MXEquipmentNoticeController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView         *listTab;
@property (nonatomic, copy)   void(^scrollCallback)(UIScrollView *scrollView);
@property (nonatomic, strong) NSMutableArray *listArray;
@property (nonatomic, assign) NSInteger page;

@property (nonatomic,strong) DHNoDataView * noDataView;
@end

@implementation MXEquipmentNoticeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.listArray = [[NSMutableArray alloc] init];
    [self createUI];
    [self getDeviceNoticeList];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadList) name:@"handleinvitesuccess" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(redHide:) name:@"redDismiss" object:nil];
}

- (void)createUI {
    [self.view addSubview:self.noDataView];
    [self.noDataView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.offset(10);
    }];
    
    [self.view addSubview:self.listTab];
    [self.listTab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.offset(10);
    }];
}

//红点消失
- (void)redHide:(NSNotification*)noti {
    for ( MXNoticeListModel *model in self.listArray)
    {
        if ([model.appNotice.ID isEqualToString:noti.object])
        {
            model.appNotice.noticeStatus = 1;
        }
    }
    [self.listTab reloadData];
}
- (void)reloadList {
    self.page = 1;
    [self getDeviceNoticeList];
}

- (void)getDeviceNoticeList {
    NSString *urlStr = [NSString stringWithFormat:@"%@?type=0&pageNum=%@&pageSize=%@",@"api/app/notice/list",@"1",@"20"];
    MJWeakSelf
    [self.view makeToastActivity];
    [[DCHttpClient shareClient] requestWithPath:urlStr params:@{} httpMethod:DCHttpRequestGet sucess:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSDictionary * dict = [responseObject mj_JSONObject];
        [weakSelf.view hideToastActivity];
        if (dict[DC_ResultCode_Key] && [dict[DC_ResultCode_Key] integerValue] == DC_Result_Success) {
            if (weakSelf.page == 1) {
                [weakSelf.listArray removeAllObjects];
            }
            NSArray *dataArray =  [NSArray modelArrayWithClass:[MXNoticeListModel class] json:dict[@"data"][@"list"]];
            [weakSelf.listArray addObjectsFromArray:dataArray];
            [weakSelf.listTab reloadData];
            
            weakSelf.noDataView.hidden = weakSelf.listArray.count > 0;
        } else {

            [weakSelf.view xjmakeToast:dict[DC_ResultMsg_Key]];
        }
    } failture:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        [weakSelf.view hideToastActivity];
    }];
}

#pragma mark - ________ UITableView ________

- (UITableView *)listTab {
    
    if (nil == _listTab) {
        _listTab = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _listTab.backgroundColor = [UIColor clearColor];
        _listTab.separatorStyle = UITableViewCellSeparatorStyleNone;
        _listTab.delegate = self;
        _listTab.dataSource = self;
        _listTab.rowHeight = 80.0;
        _listTab.showsVerticalScrollIndicator = NO;
        _listTab.showsHorizontalScrollIndicator= NO;
        [_listTab registerClass:[MXMessageListCell class] forCellReuseIdentifier:NSStringFromClass([MXMessageListCell class])];
        if (@available(iOS 11.0, *)) {
            _listTab.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        if (@available(iOS 15.0, *)) {
            _listTab.sectionHeaderTopPadding = 0;
        }
    }
    return _listTab;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    MXMessageListCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MXMessageListCell class])];
    MXNoticeListModel *model = self.listArray[indexPath.row];
    [cell reloadCellWithModel:model];
    return cell;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listArray.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MXNoticeListModel *model = self.listArray[indexPath.row];
    MXHomeInviteController *inviteVc = [[MXHomeInviteController alloc] init];
    inviteVc.model = model;
    [self.navigationController pushViewController:inviteVc animated:YES];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self dc_navBarHidden:YES];
}


#pragma mark - JXPagingViewListViewDelegate

- (UIScrollView *)listScrollView {
    return self.listTab;
}

- (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback {
    self.scrollCallback = callback;
}

- (UIView *)listView {
    return self.view;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (self.scrollCallback != nil) {
        self.scrollCallback(scrollView);
    }
}


-(DHNoDataView *)noDataView{
    if(!_noDataView){
        _noDataView = [[DHNoDataView alloc] init];
        _noDataView.hidden = YES;
        _noDataView.noDataLabel.text = @"暂无设备";
    }
    return _noDataView;
}

@end
